# -*- coding:utf-8 -*-
# --author： jingfeng 
# time: 2019/5/13


from selenium import webdriver   # 浏览器驱动对象
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys     # 键盘,控制键盘
from selenium.webdriver.support import expected_conditions as EC  # 条件
from selenium.webdriver.support.wait import WebDriverWait
import time
from lxml import etree
from selenium.webdriver.support.ui import Select
from PIL import Image
from aip import AipOcr
from io import BytesIO,StringIO

path = 'image/'

class XiYou(object):

    def __init__(self):
        option = webdriver.ChromeOptions()
        option.add_argument('disable-infobars')
        self.browser = webdriver.Chrome(options=option)
        self.browser.maximize_window()

    def login(self):

        self.browser.get('http://222.24.62.52/pyxx/login.aspx')
        user = self.browser.find_element_by_id('ctl00_txtusername')
        user.send_keys('1801210221')
        password = self.browser.find_element_by_id('ctl00_txtpassword')
        password.send_keys('czp0927')

        # 验证码
        self.browser.save_screenshot('22222.png')
        image = Image.open(r'22222.png')
        im1 = image.crop((980,370,1047,404))
        im1.save(r'captcha.png')
        number=self.get_words_by_image(r'captcha.png')
        number=number['words_result'][0]['words']
        print(number)
        captcha = self.browser.find_element_by_id('ctl00_txtyzm')

        captcha.send_keys(number)
        self.browser.find_element_by_id("ctl00_ImageButton1").click()

        try:
            alert = self.browser.switch_to_alert()
            print('登录失败')
            if alert:
                alert.accept()
                time.sleep(2)
                self.login()
        except:
            pass

    def get_words_by_image(self,path):
        with open(path,"rb") as fp:
            content= fp.read()

        options = {}
        options["language_type"] = "ENG"
        APP_ID = "10785312"
        API_KEY = "Tcp5Qqr2Elj2illfwFh3SbuG"
        SECRET_KEY = "Hw9nYBclaImtQQzagTcQhh4Vmmvx9CAq"
        client = AipOcr(APP_ID,API_KEY,SECRET_KEY)
        result = client.basicAccurate(content,options=options)
        return result


    def evaluate(self):
        self.browser.switch_to.frame('MenuFrame')
        evaluate= self.browser.find_element_by_xpath('//dl[@id="tree"]/dd['
                                                '5]/ul/li/a')
        url = evaluate.get_attribute('href')
        print(url)
        self.browser.get(url)

        time.sleep(3)

        tree = etree.HTML(self.browser.page_source)
        take_part_evaluate = tree.xpath(
            '//table[@id="MainWork_dgData"]/tbody/tr/td[6]/text()')


        for index,value in enumerate(take_part_evaluate):
            # 索引到14
            if value == '未参加':

                xpath_rlue = '//table[@id="MainWork_dgData"]/tbody/tr[%s]/td[7]/a' % (
                    index + 2)
                self.browser.find_element_by_xpath(xpath_rlue).click()

                for tr in range(17):
                    td = 'MainWork_dgData_drppjjg_%s' % tr
                    select = Select(self.browser.find_element_by_id(td))

                    select.select_by_value('优')
                text = self.browser.find_element_by_tag_name('textarea')
                text.send_keys('老师十分好，课堂内容充实，简单明了，使学生能够轻轻松松掌握知识')
                time.sleep(3)

                # 点击提交
                button = self.browser.find_element_by_xpath(
                    '//div[@id="pagestop"]/div[2]/a')
                button.click()
                time.sleep(3)
                alert = self.browser.switch_to_alert()
                alert.accept()
                time.sleep(2)
                alert = self.browser.switch_to_alert()
                alert.accept()

                quit = self.browser.find_element_by_id(
                        "MainWork_lnkExit").click()
                print('完成评价')
            else:
                pass


    def main(self):
        self.login()
        print('登录成功')
        self.evaluate()

xiyou = XiYou()
xiyou.main()
